/*
 * ExperimentalParametersPanelHandler
 * 
 * Version: 1.0: This class is responsible for generating Experimental Parameters Panel which lets users select
 * Experiments Parameters as in Tempo wherever applicable.
 * 
 * Date: 12/05/2010
 * 
 * Licensed under Eclipse open source license
 */
package slhs.tempo.scriptGenerator;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;

import javax.swing.GroupLayout;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import org.mlc.swing.layout.LayoutConstraintsManager;

public class ExperimentalParametersPanelHandler extends JPanel implements PanelHandler {

    private static final ExperimentalParametersPanelHandler experimentalParametersPanelHandler = new ExperimentalParametersPanelHandler();
    private static final int BORDER_FONT_SIZE = 14;
    public static final String VALUES_DELIMITER = ";";

    private final String[] actions = { "Do Nothing", "End Visual Display", "End Trial" };
    private final String[] FEEDBACK_OPTIONS = { "Response Box Feedback", "Word Feedback", "Image Feedback",
	    "Audio Feedback" };

    private static final String EXPERIMENTAL_PARAMETERS_PANEL_LAYOUT_XML_FILE = "EXPERIMENTAL_PARAMETERS_PANEL_LAYOUT.XML";
    private static final String TRUE = "True";
    private static final String FALSE = "False";

    private JLabel actionOnButtonPressLabel;

    private JCheckBox pauseBetweenSegmentsCheckbox;
    private JCheckBox showFeedbackOptionsCheckBox;
    private JCheckBox showStartPulseOptions;
    private JCheckBox getResponseFeedbackCheckBox;
    private JCheckBox useStartPulseCheckBox;

    private JComboBox actionOnButtonPressSelector;
    private JComboBox feedbackTypeComboBox;

    private final JPanel experimentalParametersPanel;

    private StartPulseOptionsPopup startPulseOptionsPopup;

    private final TitledBorder experimentalParametersFormatPanelBorder;

    private ExperimentalParametersPanelHandler() {

	experimentalParametersPanel = new JPanel();
	GroupLayout experimentalParametersPanelLayout = new GroupLayout(experimentalParametersPanel);
	experimentalParametersPanel.setLayout(experimentalParametersPanelLayout);

	TitledBorderFactory titledBorderFactory = new TitledBorderFactory(Color.GRAY, "sansserif", Font.BOLD
		+ Font.ITALIC, BORDER_FONT_SIZE);
	experimentalParametersFormatPanelBorder = titledBorderFactory.getTitledBorder("Experimental Parameters");
	experimentalParametersPanel.setBorder(experimentalParametersFormatPanelBorder);

	// initialize components to be added
	initComponents();
	addComponents(experimentalParametersPanelLayout);

    }

    /**
     * returns the class instance
     * */
    public static ExperimentalParametersPanelHandler getInstance() {
	return experimentalParametersPanelHandler;
    }

    private void addComponents(GroupLayout experimentalParametersPanelLayout) {
	LayoutConstraintsManager layoutConstraintsManager = LayoutConstraintsManager.getLayoutConstraintsManager(this
		.getClass().getResourceAsStream(EXPERIMENTAL_PARAMETERS_PANEL_LAYOUT_XML_FILE));
	LayoutManager panelLayout = layoutConstraintsManager.createLayout("panel", this);
	experimentalParametersPanel.setLayout(panelLayout);

	// here we add the controls to the container.
	experimentalParametersPanel.add(actionOnButtonPressSelector, "actionOnButtonPressSelector");
	experimentalParametersPanel.add(feedbackTypeComboBox, "feedbackTypeComboBox");
	experimentalParametersPanel.add(actionOnButtonPressLabel, "actionOnButtonPressLabel");
	experimentalParametersPanel.add(pauseBetweenSegmentsCheckbox, "pauseBetweenSegmentsCheckbox");
	experimentalParametersPanel.add(showFeedbackOptionsCheckBox, "showFeedbackOptions");
	experimentalParametersPanel.add(showStartPulseOptions, "showStartPulseOptions");
	experimentalParametersPanel.add(getResponseFeedbackCheckBox, "getResponseFeedbackCheckBox");
	experimentalParametersPanel.add(useStartPulseCheckBox, "useStartPulseCheckBox");

	// action listeners
	feedbackTypeComboBox.addActionListener(this);

    }

    private void initComponents() {
	actionOnButtonPressSelector = new JComboBox(actions);
	actionOnButtonPressLabel = new JLabel("Action on Button Press:");
	pauseBetweenSegmentsCheckbox = new JCheckBox("Pause between segments");
	showFeedbackOptionsCheckBox = new JCheckBox("Feedback Options");
	showStartPulseOptions = new JCheckBox("Start Pulse Options");
	getResponseFeedbackCheckBox = new JCheckBox("Get Response Feedback");
	useStartPulseCheckBox = new JCheckBox("Use Start Pulse");
	feedbackTypeComboBox = new JComboBox(FEEDBACK_OPTIONS);
	// feedbackOptionsPopup = FeedbackOptionsPopup.getInstance();
	startPulseOptionsPopup = StartPulseOptionsPopup.getInstance();

	// initial configurations
	showFeedbackOptionsCheckBox.setEnabled(false);
	showStartPulseOptions.setEnabled(false);
	feedbackTypeComboBox.setEnabled(false);

	// add action listeners
	getResponseFeedbackCheckBox.addActionListener(this);
	useStartPulseCheckBox.addActionListener(this);
	showFeedbackOptionsCheckBox.addActionListener(this);
	showStartPulseOptions.addActionListener(startPulseOptionsPopup);

    }

    @Override
    public JPanel getPanel() {
	return experimentalParametersPanel;
    }

    @Override
    public void actionPerformed(ActionEvent event) {

	Object source = event.getSource();
	if (source.equals(getResponseFeedbackCheckBox)) {
	    feedbackTypeComboBox.setEnabled(getResponseFeedbackCheckBox.isSelected());
	    // showFeedbackOptionsCheckBox.setEnabled(getResponseFeedbackCheckBox.isSelected());

	} else if (source.equals(useStartPulseCheckBox)) {
	    showStartPulseOptions.setEnabled(useStartPulseCheckBox.isSelected());
	} else if (source.equals(showFeedbackOptionsCheckBox)) {
	    FeedbackOptionsPopup feedbackOptionsPopup = FeedbackOptionsPopup.getInstance(getFeedbackType());
	    feedbackOptionsPopup.draw(showFeedbackOptionsCheckBox.isSelected(), getPopupSize());
	    showFeedbackOptionsCheckBox.setSelected(false);
	} else if (source.equals(feedbackTypeComboBox)) {
	    if (feedbackTypeComboBox.getSelectedIndex() == 0) {
		showFeedbackOptionsCheckBox.setEnabled(false);
	    } else {
		showFeedbackOptionsCheckBox.setEnabled(true);

	    }
	}
    }

    /**
     * @return
     */
    private Dimension getPopupSize() {
	Dimension dim = new Dimension(800, 470);
	if (getFeedbackType().equals(UtilityConstants.IMAGE_FEEDBACK)) {
	    dim = new Dimension(550, 200);
	} else if (getFeedbackType().equals(UtilityConstants.AUDIO_FEEDBACK)) {
	    dim = new Dimension(550, 200);
	} else {
	    dim = new Dimension(800, 470);
	}
	return dim;
    }

    private String getFeedbackType() {
	return feedbackTypeComboBox.getSelectedItem().toString();
    }

    private int getActionOnButtonPress() {
	return this.actionOnButtonPressSelector.getSelectedIndex();
    }

    private String isPauseBetweenSegments() {
	boolean isPauseBetweenSegments = this.pauseBetweenSegmentsCheckbox.isSelected();
	return ((isPauseBetweenSegments) ? TRUE : FALSE);
    }

    private String isResponseFeedbackEnabled() {
	boolean isResponseFeedbackEnabled = this.getResponseFeedbackCheckBox.isSelected();
	return ((isResponseFeedbackEnabled) ? TRUE : FALSE);
    }

    private String isStartPulseEnabled() {
	boolean isStartPulseEnabled = this.useStartPulseCheckBox.isSelected();
	return ((isStartPulseEnabled) ? TRUE : FALSE);
    }

    private String getResponseFeedbackOptions() {
	String retVal = "";
	if (getResponseFeedbackCheckBox.isSelected()) {
	    retVal += FeedbackOptionsPopup.getFeedbackOptions();
	}
	return retVal;
    }

    private String getStartPulseOptions() {
	String retVal = "";
	if (useStartPulseCheckBox.isSelected()) {
	    StartPulseOptionsPopup startPulseOptionsPopup = StartPulseOptionsPopup.getInstance();
	    retVal += startPulseOptionsPopup.getStartPulseOptions();
	}
	return retVal;
    }

    public String getExperimentalParametersString() {
	StringBuffer experimentalParameters = new StringBuffer();
	experimentalParameters.append(getActionOnButtonPress());
	experimentalParameters.append(UtilityConstants.VALUES_DELIMITER);

	experimentalParameters.append(isPauseBetweenSegments());
	experimentalParameters.append(UtilityConstants.VALUES_DELIMITER);

	// True if feedback on
	experimentalParameters.append(isResponseFeedbackEnabled());
	experimentalParameters.append(UtilityConstants.VALUES_DELIMITER);

	// append response parameter options
	experimentalParameters.append(getResponseFeedbackOptions());

	// if start pulse is disabled, this is the end and there's no need for delimiter
	if (isStartPulseEnabled() != "")
	    experimentalParameters.append(UtilityConstants.VALUES_DELIMITER);

	// append start pulse options
	experimentalParameters.append(isStartPulseEnabled());

	experimentalParameters.append(getStartPulseOptions());

	return experimentalParameters.toString();
    }
}
